package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.domain.TTracks;
import com.ldatum.crm.service.TCustomerService;
import com.ldatum.crm.service.TTrackService;
import com.ldatum.crm.serviceImpl.TCustomerServiceImpl;
import com.ldatum.crm.serviceImpl.TTrackServiceImpl;

@ParentPackage("admin")

public class TrackAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private TTracks tracks = new TTracks();
	
	TTrackService trackService = new TTrackServiceImpl();
	TCustomerService customerService =  new TCustomerServiceImpl();
	
	List<TTracks> tracksList = new ArrayList<TTracks>();
	List<TCustomer> customerList = new ArrayList<TCustomer>();
	
	private int trackId;
	private int customerId;
	private String customerName;
	private String theme;
	private String contactsName;
	private String trackTime;
	private String trackWays;
	private String beforeTrackStep;
	private String afterTrackStep;
	private String catagory;
	private String trackDetail;
	private String nextTrackTime;
	private String createDateTime;
	private String tracker;
	
	private String levelOne;
	private String levelTwo;
	
	public void findById(){

		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			tracks = trackService.findById(TTracks.class, trackId);
			customerId = tracks.getCustomer().getId();
			customerName = tracks.getCustomer().getShortName();
			tracks.setCustomer(null);
			json.accumulate("tracks", tracks);
			json.accumulate("customerId", customerId);
			json.accumulate("customerName", customerName);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void save(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			TCustomer customer = new TCustomer();
			customer.setId(customerId);
			tracks.setCustomer(customer);
			trackService.save(tracks);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String list() throws Exception{
		tracksList = trackService.list(TTracks.class,(String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME)); 
		levelOne = "customer";
		levelTwo = "tracks";
		return "listTracks";
	}
	
	public String add() throws Exception{
		String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
		customerList = customerService.myCustomer(TCustomer.class,currentEmployee);
		System.out.println(customerList.get(3).getShortName());
		tracker = currentEmployee;
		levelOne = "customer";
		levelTwo = "tracks";
		return "addTrack";
	}

	public TTracks getTracks() {
		return tracks;
	}


	public void setTracks(TTracks tracks) {
		this.tracks = tracks;
	}


	public TTrackService getTrackService() {
		return trackService;
	}


	public void setTrackService(TTrackService trackService) {
		this.trackService = trackService;
	}
	public int getTrackId() {
		return trackId;
	}

	public void setTrackId(int trackId) {
		this.trackId = trackId;
	}

	public String getTheme() {
		return theme;
	}


	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getTrackWays() {
		return trackWays;
	}


	public void setTrackWays(String trackWays) {
		this.trackWays = trackWays;
	}

	public String getCatagory() {
		return catagory;
	}


	public void setCatagory(String catagory) {
		this.catagory = catagory;
	}

	public List<TTracks> getTracksList() {
		return tracksList;
	}

	public void setTracksList(List<TTracks> tracksList) {
		this.tracksList = tracksList;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getContactsName() {
		return contactsName;
	}

	public void setContactsName(String contactsName) {
		this.contactsName = contactsName;
	}

	public String getTrackTime() {
		return trackTime;
	}

	public void setTrackTime(String trackTime) {
		this.trackTime = trackTime;
	}

	public String getBeforeTrackStep() {
		return beforeTrackStep;
	}

	public void setBeforeTrackStep(String beforeTrackStep) {
		this.beforeTrackStep = beforeTrackStep;
	}

	public String getAfterTrackStep() {
		return afterTrackStep;
	}

	public void setAfterTrackStep(String afterTrackStep) {
		this.afterTrackStep = afterTrackStep;
	}

	public String getTrackDetail() {
		return trackDetail;
	}

	public void setTrackDetail(String trackDetail) {
		this.trackDetail = trackDetail;
	}

	public String getNextTrackTime() {
		return nextTrackTime;
	}

	public void setNextTrackTime(String nextTrackTime) {
		this.nextTrackTime = nextTrackTime;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public String getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(String createDateTime) {
		this.createDateTime = createDateTime;
	}

	public String getTracker() {
		return tracker;
	}

	public void setTracker(String tracker) {
		this.tracker = tracker;
	}

	public List<TCustomer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}
	
}
